package com.saozhu.command;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import com.saozhu.Mimpl.ManagerAddGoodsCommand;
import com.saozhu.Mimpl.ManagerAlterGoodsCommand;
import com.saozhu.Mimpl.ManagerDeleteGoodsCommand;
import com.saozhu.Mimpl.ManagerDeleteOrderCommand;
import com.saozhu.Mimpl.ManagerExitCommand;
import com.saozhu.Mimpl.ManagerHelpCommand;
import com.saozhu.Mimpl.ManagerShowCommand;
import com.saozhu.pojo.Player;

public class ManagerCommandFactory extends AbstractCommandFactory {
	
	private Player player;
	
	public ManagerCommandFactory(Player player) {
		this.player = player;
	}

	public static Map<CommandCode, Class<? extends Command>> commandMap = new HashMap<>();
	static { 
		commandMap.put(CommandCode.ADDGOODS, ManagerAddGoodsCommand.class);
		commandMap.put(CommandCode.ALTERGOODS, ManagerAlterGoodsCommand.class);
		commandMap.put(CommandCode.DELETEGOODS, ManagerDeleteGoodsCommand.class);
		commandMap.put(CommandCode.DELETEORDER, ManagerDeleteOrderCommand.class);
		commandMap.put(CommandCode.EXIT, ManagerExitCommand.class);
		commandMap.put(CommandCode.HELP, ManagerHelpCommand.class);
		commandMap.put(CommandCode.SHOW, ManagerShowCommand.class);
	}

	@Override
	public Command buildCommand(CommandCode commandcode) {
		Class<? extends Command> clazz = commandMap.get(commandcode);

		try {
			Constructor<? extends Command> c = clazz.getConstructor(Player.class);
			Command command = c.newInstance(player);
			return command;
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return null;
	}

}
